package com.bit.cfdi.web.vm.comprobante;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Window;

import com.bit.cfdi.web.comprobante.utils.ConceptosData;
import com.bit.cfdi.web.domain.Cliente;
import com.bit.cfdi.web.domain.Comprobante;
import com.bit.cfdi.web.domain.ComprobanteConcepto;
import com.bit.cfdi.web.domain.Concepto;
import com.bit.cfdi.web.domain.DatosFiscales;
import com.bit.cfdi.web.domain.Direccion;
import com.bit.cfdi.web.domain.Estado;
import com.bit.cfdi.web.domain.Pais;
import com.bit.cfdi.web.services.EstadoService;
import com.bit.cfdi.web.services.PaisService;
import com.bit.cfdi.web.session.SessionUtils;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class FacturaViewModel {

	private Logger log = LogManager.getLogger(this.getClass().getName());
	
	private Comprobante comprobante;
	private Cliente cliente;
	private String pais;
	private ConceptosData conceptosData = new ConceptosData();
	private ComprobanteConcepto comprobanteConcepto;
	
	private Boolean chkParte;

	@WireVariable
	private EstadoService estadoService;
	@WireVariable
	private PaisService paisService;
	
	
	@Init
	public void init(){
		chkParte = false;
		cliente = new Cliente();
		cliente.setDatosFiscales(new DatosFiscales());		
		cliente.getDatosFiscales().setDireccion(new Direccion());
		
		comprobanteConcepto = new ComprobanteConcepto();
		comprobanteConcepto.setConcepto(new Concepto());
	}
	
	@WireVariable
	private SessionUtils sessionUtils;

	public Comprobante getComprobante() {
		return comprobante;
	}

	public void setComprobante(Comprobante comprobante) {
		this.comprobante = comprobante;
	}
	
	public Cliente getCliente() {
		return cliente;
	}

	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}		

	public ComprobanteConcepto getComprobanteConcepto() {
		return comprobanteConcepto;
	}

	public void setComprobanteConcepto(ComprobanteConcepto comprobanteConcepto) {
		this.comprobanteConcepto = comprobanteConcepto;
	}

	public ConceptosData getConceptosData() {
		return conceptosData;
	}

	public String getPais() {
		return pais;
	}

	@NotifyChange({"estados"})
	public void setPais(String pais) {		
		this.pais = pais;
	}

	public List<String> getPaises(){
		List<String> paises = new ArrayList<String>();		
		
		for(Pais p: paisService.getAll()){
			paises.add(p.getPais());
		}
		
		return paises;
	}
	
	public List<String> getEstados(){
		List<String> edos = new ArrayList<String>();
		cliente.getDatosFiscales().getDireccion().setEstado(null);
				
		List<Estado> edoList = estadoService.getEstadosByPais(getPais());
		if(null!=edoList && edoList.size()>0)
			for(Estado e: edoList){
				edos.add(e.getEstado());
			}
				
		return  edos;
	}

	@Command
	@NotifyChange("*")
	public void getDataComprobante(){		
		
		cliente.getDatosFiscales().getDireccion().setPais(getPais());

		
		System.out.println("Test");
	}
		
	public Boolean getChkParte() {
		return chkParte;
	}

	public void setChkParte(Boolean chkParte) {
		this.chkParte = chkParte;
	}

	@Command
	@NotifyChange("*")
	public void addConcepto(){
		
		comprobanteConcepto.getCantidad();
		
		conceptosData.addConcepto(comprobanteConcepto);
		
		comprobanteConcepto = new ComprobanteConcepto();
		comprobanteConcepto.setConcepto(new Concepto());
		
		System.out.println("Test");
	}
	
	@Command 
	public void addPartConcep()
	{
		if(chkParte)
		{
			Window window =(Window)Executions.createComponents(
				"/home/comprobante/modalWindowAddParte.zul", null, null);
			window.doModal();
		}
	}
	
}
